<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="客户名称" prop="customername">
      <el-input v-model="dataForm.customername" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="客户地址" prop="address">
      <el-input v-model="dataForm.address" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="客户电话" prop="telephone">
      <el-input v-model="dataForm.telephone" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="邮编" prop="zip">
      <el-input v-model="dataForm.zip" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="connectionpersion">
      <el-input v-model="dataForm.connectionpersion" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="联系人电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="开户银行" prop="bank">
      <el-input v-model="dataForm.bank" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="银行账号" prop="account">
      <el-input v-model="dataForm.account" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="传真" prop="fax">
      <el-input v-model="dataForm.fax" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="是否可用" prop="available">
        <el-switch
          style="display: block;line-height: 28px;"
          v-model="dataForm.available"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="可用"
          inactive-text="不可用"
          :active-value="1"
          :inactive-value="0">
        </el-switch>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          customername: '',
          zip: '',
          address: '',
          telephone: '',
          connectionpersion: '',
          phone: '',
          bank: '',
          account: '',
          email: '',
          fax: '',
          available: ''
        },
        dataRule: {
          customername: [
            { required: true, message: '请输入客户名称', trigger: 'blur' },
            { min: 2, max: 20, message: '客户名称长度在 2 到 20 个字符', trigger: 'blur'}
          ],
          zip: [
            { required: true, message: '请输入邮编', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '请输入客户地址', trigger: 'blur' }
          ],
          telephone: [
            { required: true, message: '请输入客户电话', trigger: 'blur' },
             { validator:function(rule,value,callback){
                            if(/^1[3-9][0-9]{9}$/.test(value) == false){
                                   callback(new Error("请输入正确的客户电话"));
                            }else{
                                   callback();
                            }
                          }, trigger: 'blur'
                }
          ],
          connectionpersion: [
            { required: true, message: '请输入联系人姓名', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '请输入联系人电话', trigger: 'blur' },
             { validator:function(rule,value,callback){
                            if(/^1[3-9][0-9]{9}$/.test(value) == false){
                                   callback(new Error("请输入正确的联系人电话"));
                            }else{
                                   callback();
                            }
                          }, trigger: 'blur'
                }
          ],
          bank: [
            { required: true, message: '请输入开户银行', trigger: 'blur' }
          ],
          account: [
            { required: true, message: '请输入银行账号', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur'}
          ],
          fax: [
            { required: true, message: '请输入传真', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/warehouse/customer/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.customername = data.customer.customername
                this.dataForm.zip = data.customer.zip
                this.dataForm.address = data.customer.address
                this.dataForm.telephone = data.customer.telephone
                this.dataForm.connectionpersion = data.customer.connectionpersion
                this.dataForm.phone = data.customer.phone
                this.dataForm.bank = data.customer.bank
                this.dataForm.account = data.customer.account
                this.dataForm.email = data.customer.email
                this.dataForm.fax = data.customer.fax
                this.dataForm.available = data.customer.available
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/warehouse/customer/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'customername': this.dataForm.customername,
                'zip': this.dataForm.zip,
                'address': this.dataForm.address,
                'telephone': this.dataForm.telephone,
                'connectionpersion': this.dataForm.connectionpersion,
                'phone': this.dataForm.phone,
                'bank': this.dataForm.bank,
                'account': this.dataForm.account,
                'email': this.dataForm.email,
                'fax': this.dataForm.fax,
                'available': this.dataForm.available
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
